import { createRouter, createWebHistory } from 'vue-router'
import { initUserState, isLoggedIn, currentRole } from '@/store/user'

// 通用页面
import Login from '../views/Login.vue'
import MainLayout from '../components/layout/MainLayout.vue'

// 管理端页面
import AdminHome from '../views/admin/Home.vue'

// 医疗保险基本信息维护模块 - 直接使用业务页面
import DrugList from '../views/medical-info/drugs/DrugList.vue'
import DrugEdit from '../views/medical-info/drugs/DrugEdit.vue'
import TreatmentIndex from '../views/medical-info/treatment/index.vue'
import TreatmentList from '../views/medical-info/treatment/TreatmentList.vue'
import TreatmentEdit from '../views/medical-info/treatment/TreatmentEdit.vue'
import FacilityList from '../views/medical-info/facilities/FacilityList.vue'
import FacilityEdit from '../views/medical-info/facilities/FacilityEdit.vue'
import DrugRatioRule from '../views/medical-info/drug-ratio/Rule.vue'
import HospitalRatioIndex from '../views/medical-info/hospital-ratio/index.vue'
import Level3 from '../views/medical-info/hospital-ratio/Level3.vue'
import Level2 from '../views/medical-info/hospital-ratio/Level2.vue'
import Level1 from '../views/medical-info/hospital-ratio/Level1.vue'

// 医院医生站模块 - 直接使用业务页面
import PatientSelect from '../views/hospital-doctor/diagnosis/PatientSelect.vue'
import AdmissionRegister from '../views/hospital-doctor/diagnosis/AdmissionRegister.vue'
import AdmissionDiagnosis from '../views/hospital-doctor/diagnosis/AdmissionDiagnosis.vue'
import PrimaryDiagnosis from '../views/hospital-doctor/diagnosis/PrimaryDiagnosis.vue'
import OtherDiagnosis from '../views/hospital-doctor/diagnosis/OtherDiagnosis.vue'
import DrugOrders from '../views/hospital-doctor/prescription/DrugOrders.vue'
import TreatmentOrders from '../views/hospital-doctor/treatment-order/TreatmentOrders.vue'
import MedicalServiceOrders from '../views/hospital-doctor/service-order/MedicalServiceOrders.vue'

// 医保中心模块
import InsuranceCenterPersonnel from '../views/insurance-center/personnel/index.vue'
import PersonnelList from '../views/insurance-center/personnel/list.vue'
import InsuranceCenterCostQuery from '../views/insurance-center/cost-query/index.vue'
import CostQuerySearch from '../views/insurance-center/cost-query/search.vue'
import InsuranceCenterReimbursementReport from '../views/insurance-center/reimbursement-report/index.vue'
import InsuranceCenterReimbursement from '../views/insurance-center/reimbursement/index.vue'
import ReimbursementList from '../views/insurance-center/reimbursement/list.vue'

// 系统管理模块 - 直接使用业务页面
import UserManagement from '../views/system/user/User.vue'
import SystemRole from '../views/system/role/index.vue'
import SystemMenu from '../views/system/menu/index.vue'



const routes = [
  // 通用登录路由（独立页面，不使用MainLayout）
  { path: '/login', component: Login },
  
  // 主应用布局（包含所有业务页面）
  {
    path: '/',
    component: MainLayout,
    redirect: '/medical-info',
    children: [
      // 医疗保险基本信息维护模块路由
      { path: 'medical-info', redirect: '/medical-info/drugs' },
      { path: 'medical-info/drugs', component: DrugList },
      { path: 'medical-info/drugs/edit/:id?', component: DrugEdit },
      { path: 'medical-info/treatment', component: TreatmentList },
      { path: 'medical-info/treatment/index', component: TreatmentIndex },
      { path: 'medical-info/treatment/list', component: TreatmentList },
      { path: 'medical-info/treatment/edit/:id?', component: TreatmentEdit },
      { path: 'medical-info/facilities', component: FacilityList },
      { path: 'medical-info/facilities/edit/:id?', component: FacilityEdit },
      { path: 'medical-info/drug-ratio', component: DrugRatioRule },
      { path: 'medical-info/hospital-ratio', component: HospitalRatioIndex },
      { path: 'medical-info/hospital-ratio-3', component: Level3 },
      { path: 'medical-info/hospital-ratio-2', component: Level2 },
      { path: 'medical-info/hospital-ratio-1', component: Level1 },
      
      // 医院住院医生站模块路由
      { path: 'hospital-doctor', redirect: '/hospital-doctor/admission-register' },
      { path: 'hospital-doctor/admission-register', component: AdmissionRegister },
      
      // 患者诊断模块
      { path: 'hospital-doctor/patient-select', component: PatientSelect, meta: { title: '患者选择', requiresAuth: true } },
      { path: 'hospital-doctor/admission-diagnosis', component: AdmissionDiagnosis },
      { path: 'hospital-doctor/primary-diagnosis', component: PrimaryDiagnosis },
      { path: 'hospital-doctor/other-diagnosis', component: OtherDiagnosis },
      
      // 患者医嘱模块
      { path: 'hospital-doctor/patient-select-orders', component: PatientSelect, meta: { title: '患者选择(医嘱)', requiresAuth: true } },
      { path: 'hospital-doctor/drug-orders', component: DrugOrders },
      { path: 'hospital-doctor/treatment-orders', component: TreatmentOrders },
      { path: 'hospital-doctor/medical-service-orders', component: MedicalServiceOrders },
      
      // 医保中心报销管理模块路由
      { path: 'insurance-center', redirect: '/insurance-center/personnel' },
      { path: 'insurance-center/personnel', component: PersonnelList },
      { path: 'insurance-center/personnel/intro', component: InsuranceCenterPersonnel },
      { path: 'insurance-center/cost-query', component: CostQuerySearch },
      { path: 'insurance-center/cost-query/intro', component: InsuranceCenterCostQuery },
      { path: 'insurance-center/cost-query/search', component: CostQuerySearch },
      { path: 'insurance-center/reimbursement-report', component: InsuranceCenterReimbursementReport },
      { path: 'insurance-center/reimbursement', component: ReimbursementList },
      { path: 'insurance-center/reimbursement/intro', component: InsuranceCenterReimbursement },
      { path: 'insurance-center/reimbursement/list', component: ReimbursementList },
      
      // 系统管理模块路由
      { path: 'system', redirect: '/system/user' },
      { path: 'system/user', component: UserManagement },
      { path: 'system/role', component: SystemRole },
      { path: 'system/menu', component: SystemMenu },
      
      // 保留的管理端和医院端路由
      { path: 'admin', component: AdminHome }
    ]
  },
  
  // 兼容旧路由的重定向
  { path: '/admin/login', redirect: '/login' },
  { path: '/hospital/login', redirect: '/login' },
  { path: '/claim/login', redirect: '/login' }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  // 初始化用户状态
  initUserState()
  
  const token = localStorage.getItem('token')
  
  // 如果访问登录页面，直接允许
  if (to.path === '/login') {
    next()
    return
  }
  
  // 如果没有token，重定向到登录页
  if (!token) {
    next('/login')
    return
  }
  
  // 如果有token，允许访问
  next()
})

export default router